<?php
/**
 * 轻舟CMS(QZLOG)
 *
 * 一款基于PHP+MYSQL的开源内容管理系统，模板标签简单、可扩展性强，利于二次开发，轻松建站的首选利器。
 *
 * @package     QZLOG
 * @license     http://www.qzlog.com/license
 * @link        http://www.qzlog.com
 * @mail        info@shyohan.com
 * @since       Version 3.0
 * @copyright   Copyright (c) 2015 - 2017 上海优翰信息科技有限公司. All rights reserved.
 */

defined('IN_QZLOG') OR exit('QZLOG:No direct script access allowed!');

/**
 * 会员管理模型
 * @author LINK <link@shyohan.com>
 */
class MemberModel extends ViewModel
{
    public $table = "member";

    /**
     * 表单验证
     * LINK <link@shyohan.com>
     * @var array
     */
    public $validate = array(
        array('username', 'nonull', '用户名不能为空 ', 2, 1),
        array('username', 'IsUsername', '用户名已经存在 ', 2, 1),
        array('password', 'minlen:5', '密码不能小于5位 ', 3, 3),
        array('password', 'nonull', '密码不能为空 ', 3, 3),
        array('password', 'confirm:repassword', '密码不一致 ', 3, 3)
    );

    /**
     * 验证账号
     * LINK <link@shyohan.com>
     * @param $name
     * @param $value
     * @param $msg
     * @param $arg
     * @return bool
     */
    public function IsUsername($name, $value, $msg, $arg)
    {
        $map['username'] = $value;
        if (M('member')->find($map)) {
            return $msg;
        } else {
            return true;
        }
    }

    /**
     * 添加用户
     * LINK <link@shyohan.com>
     * @return bool
     */
    public function addUser()
    {
        if ($this->create()) {
            $code = $this->getUserCode();
            $this->data['code'] = $code;
            $this->data['password'] = md5($this->data['password'] . $code);
            $this->data['nickname'] = $this->data['username'];
            $this->data['reg_time'] = time();
            $this->data['last_login_time'] = time();
            $this->data['reg_ip'] = ip_get_client();
            $this->data['last_login_ip'] = ip_get_client();
            $this->data['status'] = 1;
            if ($this->add()) {
                return true;
            } else {
                $this->error = '添加失败';
                return false;
            }
        }
    }

    /**
     * 修改用户
     * LINK <link@shyohan.com>
     * @return bool
     */
    public function editUser()
    {
        if ($this->create()) {
            //没有添加密码时删除密码数据
            if (empty($this->data['password'])) {
                unset($this->data['password']);
            } else {
                $code = $this->getUserCode();
                $this->data['code'] = $code;
                $this->data['password'] = md5($this->data['password'] . $code);
            }
            if ($this->save()) {
                return true;
            } else {
                $this->error = '修改失败';
            }
        }
    }

    /**
     * 删除用户
     * LINK <link@shyohan.com>
     * @return bool
     */
    public function delUser()
    {
        $id = Q("id", 0);
        if (!$id) {
            $this->error = '参数错误';
            return false;
        }
        if (in_array(1, explode(',', $id))) {
            $this->error = '不允许删除超级管理员';
            return false;
        }
        $map['id'] = array('in', explode(",", $id));
        if ($this->where($map)->del()) {
            return true;
        } else {
            $this->error = '删除失败';
        }
    }

    /**
     * 改变用户状态
     * LINK <link@shyohan.com>
     * @return bool
     */
    public function changeStatusUser()
    {
        $id = Q("id", 0);
        $method = Q("method");
        if (!$id) {
            $this->error = '请选择要操作的数据';
            return false;
        }
        if (in_array(1, explode(',', $id))) {
            $this->error = '不允许操作超级管理员';
            return false;
        }
        switch (strtolower($method)) {
            case 'forbid':
                if ($this->where(array('id' => array('in', explode(",", $id))))->save(array('status' => 0))) {
                    return true;
                } else {
                    $this->error = '禁用失败!';
                    return false;
                }
                break;
            case 'resume':
                if ($this->where(array('id' => array('in', explode(",", $id))))->save(array('status' => 1))) {
                    return true;
                } else {
                    $this->error = '恢复失败!';
                    return false;
                }
                break;
            default:
                $this->error = '参数非法';
                return false;
        }
    }

    /**
     * 获取用户密码加密key
     * LINK <link@shyohan.com>
     * @return string
     */
    public function getUserCode()
    {
        return substr(md5(mt_rand(1, 1000) . time()), 0, 10);
    }
}